/*
 * @Description: Time util
 * @Author: zhaokun
 * @Date: 2022-01-07 10:05:17
 * @LastEditTime: 2022-01-10 09:11:37
 * @FilePath: \user\common\nwTime.go
 */
package common

import (
	"time"
)

const TIME_LAYOUT = "2006-01-02 15:04:05"
const DATE_LAYOUT = "2006-01-02"

type TimeUtil interface {
	NowFormat() string
	DateFormat(timestamp int64) string
	TimeFormat(timestamp int64) string
	DateTimeFormat(timestamp int64) string
	NowTimestamp() int64
	strToTime(timeStr string) int64
	after(timeStr string) bool
	before(timeStr string) bool
}

type nwTimeUtil struct {
}

func NewNwTimeUtil() *nwTimeUtil {
	return &nwTimeUtil{}
}

/**
 * @description: 获取当前日期时间格式化字符串
 * @param
 * @return
 */

func (timeUtil *nwTimeUtil) NowFormat() string {
	return time.Now().Format(TIME_LAYOUT)
}

/**
 * @description: 时间戳格式化日期
 * @param {int64} timestamp
 * @return {*}
 */
func (timeUtil *nwTimeUtil) DateFormat(timestamp int64) string {
	if timestamp == 0 {
		timestamp = time.Now().Unix()
	}
	return time.Unix(timestamp, 0).Format(DATE_LAYOUT)
}

/**
 * @description: 时间戳格式化日期时间
 * @param {int64} timestamp
 * @return string
 */
func (timeUtil *nwTimeUtil) TimeFormat(timestamp int64) string {
	if timestamp == 0 {
		timestamp = time.Now().Unix()
	}
	return time.Unix(timestamp, 0).Format(TIME_LAYOUT)
}

/**
 * @description: 自定义格式化日期时间
 * @param {int64} timestamp
 * @param {string} formart
 * @return string
 */
func (timeUtil *nwTimeUtil) DateTimeFormat(timestamp int64, formart string) string {
	if timestamp == 0 {
		timestamp = time.Now().Unix()
	}
	if formart == "" {
		formart = TIME_LAYOUT
	}
	return time.Unix(timestamp, 0).Format(formart)
}

/**
 * @description: 返回当前时间戳（秒）
 * @return int64
 */
func (timeUtil *nwTimeUtil) NowTimestamp() int64 {
	return time.Now().Unix()
}

/**
 * @description: 返回日期时间戳
 * @param {string} timeStr
 * @return int64
 */
func (timeUtil *nwTimeUtil) TimeToSecond(timeA time.Time) int64 {
	return timeA.Unix()
}

/**
 * @description: 返回字符串日期时间戳
 * @param {string} timeStr
 * @return int64
 */
func (timeUtil *nwTimeUtil) StrToTime(timeStr string) int64 {
	// timeTem, err := time.Parse(TIME_LAYOUT, timeStr)
	timeTem, err := time.ParseInLocation("2006-01-02 15:04:05", timeStr, time.Local)
	if err != nil {
		return 0
	}
	return timeTem.Unix()
}

/**
 * @description: 返回日期A是否在日期B之后
 * @param {time.Time} timeA
 * @param {time.Time} timeB
 * @return bool
 */
func (timeUtil *nwTimeUtil) After(timeA time.Time, timeB time.Time) bool {
	isAfter := false
	boolAfter := timeA.After(timeB)
	if boolAfter {
		isAfter = true
	}
	return isAfter
}

/**
 * @description: 返回日期A是否在日期B之前
 * @param {time.Time} timeA
 * @param {time.Time} timeB
 * @return bool
 */
func (timeUtil *nwTimeUtil) Before(timeA time.Time, timeB time.Time) bool {
	isBefore := false
	boolBefore := timeA.Before(timeB)
	if boolBefore {
		isBefore = true
	}
	return isBefore
}
